/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package newkfp;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author labib.farag
 */
@Stateless
public class CustomerFacade extends AbstractFacade<Customer> {

    @PersistenceContext(unitName = "KFPNEWPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public CustomerFacade() {
        super(Customer.class);
    }

    public List<Customer> searchCustomers(String inputValue) {
        System.out.println("//Input Value: " + inputValue);
        Query query = getEntityManager().createNamedQuery("Customer.findByCustomerNameCustomerCode");
        query.setParameter("inputValue", inputValue + "%");
        List<Customer> customers = query.getResultList();
        System.out.println("//List Size= " + customers.size());
        return customers;
    }

    public Customer findByID(int i) {
        Query query = getEntityManager().createNamedQuery("Customer.findByIdCustomerID");
        query.setParameter("idCustomer", i);
        Customer customers = (Customer) query.getSingleResult();
        return customers;
    }

    public Customer findByName(String customerName) {
        Query query = getEntityManager().createNamedQuery("Customer.findByCustomerByName");
        query.setParameter("customerName", customerName);
        Customer customers = (Customer) query.getSingleResult();
        return customers;
    }
}
